package cn.tedu.csmall.sentinel.study;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class SentinelStudyApplication {
    public static void main(String[] args) {
        SpringApplication.run(SentinelStudyApplication.class,args);
        //编写 当前sentinel中唯一资源sayHi的流控规则 /熔断规则
        //针对资源的规则,一个程序可以有多个规则对象,流控规则FlowRule
        //List<DegradeRule> degradeRules=new ArrayList<>();
        //添加一个熔断规则
        //DegradeRule degradeRule=new DegradeRule();
        //规则对应限制的资源id
        /*degradeRule.setResource("sayHi");
        degradeRule.setCount(1);
        degradeRule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
        degradeRule.setTimeWindow(10);//如果出现熔断,持续多长时间重试
        degradeRule.setSlowRatioThreshold(0.5);
        degradeRule.setMinRequestAmount(1);
        degradeRule.setStatIntervalMs(10000);//统计的时间范围
        degradeRules.add(degradeRule);
        DegradeRuleManager.loadRules(degradeRules);*/
        /*List<FlowRule> rules=new ArrayList<>();
        //定义一个具体的流控规则,放到list中
        FlowRule flowRule=new FlowRule();
        flowRule.setResource("sayHi");
        //设置qps上限是1,count就是阈值
        flowRule.setCount(1);
        //设置限流阈值类型 1 = qps 0 = 并发
        flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        //总结上述规则表示的含义: 对sayHi的资源 设置一个流控规则,qps数值超过1/s就实现限流
        //抛出调用资源的BlockException异常
        rules.add(flowRule);
        //将规则列表加载到程序内容
        FlowRuleManager.loadRules(rules);*/
    }
}
